package com.bw.polling.view

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.polling.R
import com.bw.polling.databinding.ActivityPollingBinding
import com.bw.polling.entity.PollingEntity
import com.bw.polling.net.PollingViewModel
import com.example.module_service.adapter.XunluoAdapter
/**
 *  智慧园区-07 -点位管理 -v1.1-张梦停
 *  2025/05/13
 */
@Route(path = RoutePath.PATH_POLLING)
class PollingActivity : BaseMvvmActivity<ActivityPollingBinding,PollingViewModel>() {
    lateinit var xunluoAdapter: XunluoAdapter
    private val originNewList = mutableListOf<PollingEntity>() // 原始数据
    override fun initView() {
        binding.topBar.ivBack.setOnClickListener { finish() }
        binding.topBar.tvLeftTitle.text = "点位管理"
        binding.topBar.tvLeftTitle.visibility = View.VISIBLE
        binding.topBar.tvTitle.visibility = View.GONE
        binding.topBar.ivAdd.visibility = View.GONE
        binding.topBar.ivMore.visibility = View.VISIBLE

        xunluoAdapter = XunluoAdapter()
        binding.re.adapter = xunluoAdapter
        binding.re.layoutManager = LinearLayoutManager(this)
        binding.floatBtn.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_ADD_POLLING).navigation()
        }
        xunluoAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/poll/detail").navigation()
        }
        xunluoAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除条目")
                .setMessage("是否删除当前条目")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", DialogInterface.OnClickListener { dialogInterface, i ->
                    vm.delPolling(xunluoAdapter.data[position].id)
                    xunluoAdapter.removeAt(position)
                    xunluoAdapter.notifyItemRemoved(position)
                })
                .show()
            true
        }
        binding.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
            override fun afterTextChanged(s: Editable?) {
                val query = s.toString()
                val filteredList = if (query.isEmpty()) {
                    originNewList
                } else {
                    originNewList.filter {
                        it.patrolType.contains(query, ignoreCase = true)||
                                it.pointLocation.contains(query, ignoreCase = true)
                    }
                }
                xunluoAdapter.data.clear()
                xunluoAdapter.data.addAll(filteredList)
                xunluoAdapter.notifyDataSetChanged()
            }
        })
    }

    override fun initData() {
//        vm.getPolling()
        vm.pollingSuccess.observe(this){
//            ToastUtils.showShort(it.size)
            originNewList.clear()
            originNewList.addAll(it)
            xunluoAdapter.data.addAll(originNewList)
            xunluoAdapter.data.reverse()
            xunluoAdapter.notifyDataSetChanged()
        }
        vm.DeletePollingSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }

    }

    override fun onResume() {
        super.onResume()
        vm.getPolling()
    }

}